package com.broada.apm.logic;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.ws.rs.QueryParam;

import com.broada.apm.ajax.Result;
import com.broada.apm.entity.NoticeCommandException;
import com.broada.apm.entity.SSLCertificate;
import com.broada.apm.entity.Service;
import com.broada.apm.entity.ServiceLua;
import com.broada.apm.indicator.api.common.ApmDataIndicatorException;
import com.broada.apm.indicator.api.common.DisServiceReferredException;
import com.broada.apm.indicator.api.common.ServiceNotExistException;
import com.broada.apm.model.LuaSettings;

public interface ServerLogic {

	/**
	 * 根据节点id查询服务节点
	 * @param serviceId
	 * @return
	 */
	public Service queryServiceById(String serviceId);

	/**
	 * 根据关键词查询服务节点
	 * @param keyWord
	 * @param appId
	 * @param orderBy
	 * @param asc
	 * @return
	 */
	public List<Service> queryDisServiceByKeyWord(String keyWord, String appId, String orderBy, boolean asc);

	/**
	 * 删除服务节点
	 * @param serviceId
	 */
	public void deleteServiceById(String serviceId) throws Exception;
	
	/**
	 * 删除服务节点
	 * @param serviceIds
	 */
	public void deleteServiceByIds(String[] serviceIds);

	/**
	 * 保存服务节点
	 * @param service
	 */
	public void saveService(Service service);

	/**
	 * 修改节点名称
	 * 
	 * @param id
	 * @param scope
	 * @param ser
	 * @throws ApmDataIndicatorException 
	 * @throws ServiceNotExistException 
	 */
	public void updateServiceName(Service service) throws ApmDataIndicatorException, ServiceNotExistException;
	
	/**
	 * 修改节点协议
	 * @param service
	 * @return
	 * @throws ApmDataIndicatorException
	 * @throws NoticeCommandException
	 */
	public Service updateServiceProtocol(Service service) throws ApmDataIndicatorException, NoticeCommandException;

	/**
	 * 删除自动发现节点
	 * 
	 * @param id
	 * @throws DisServiceReferredException 
	 * @throws ApmDataIndicatorException 
	 */
	public void deleteDiscoveryService(String id) throws Exception;

	/**
	 * 查询服务节点列表
	 * @param keyWord
	 * @param serviceId
	 * @param direction
	 * @param appId
	 * @param orderBy
	 * @param asc
	 * @return
	 * @throws ApmDataIndicatorException
	 */
	public List<Service> queryDisServiceByCondition(String keyWord, String serviceId, String direction, String appId, String orderBy, boolean asc) throws ApmDataIndicatorException  ;
	
	/**
	 * 查询下级业务节点
	 * @param currentId
	 * @return
	 * @throws QueryNextServiceException 
	 * @throws ApmDataIndicatorException 
	 */
	public List<Service> queryNextService(String serviceId) throws ApmDataIndicatorException;
	
	/**
	 * 查询上级业务节点
	 * @param currentId
	 * @return
	 * @throws QueryLastServiceException 
	 * @throws ApmDataIndicatorException 
	 */
	public List<Service> queryLastService(String serviceId, String appId) throws ApmDataIndicatorException;

	/**
	 * 根据应用系统查询服务节点列表
	 * @param appId
	 * @return
	 */
	public List<Service> queryServiceByAppId(String appId);

	/**
	 * 更新服务节点
	 * @param service
	 */
	public void updateService(Service service);

	/**
	 * 查询集群中的
	 * @param clusterId
	 * @return
	 */
	public List<Service> queryChildServiceByApplicationId(String clusterId);

	/**
	 * 手动添加节点
	 * @param service
	 */
	public void saveDisService(Service service);

	/**
	 * 修改自动发现节点
	 * @param disService
	 */
	public void updateDisService(Service disService);

	/**
	 * 查找自动发现的节点
	 * @param serviceId
	 * @return
	 */
	public Service queryDisServiceById(String serviceId);
	
	/**
	 * 根据IP+PORT查找节点
	 * @param ip
	 * @param port
	 * @return
	 */
	public List<Service> queryDisServiceByIpAndPort(String ip, int port);

	/**
	 * 根据集群节点id或自身id获取节点
	 * @param serviceIds
	 * @return
	 */
	public List<Service> queryServicesByIds(String[] serviceIds);

	/**
	 * 保存lua代码
	 * @param service
	 * @throws Exception
	 */
	public void updateServiceLua(Service service) throws Exception;

	/**
	 * 保存ssl证书
	 * @param sslCertificate
	 * @throws Exception
	 */
	public void saveSSLCertificate(SSLCertificate sslCertificate) throws Exception;

	/**
	 * 通过节点id查询ssl证书
	 * @param serviceId
	 * @return
	 * @throws Exception 
	 */
	public SSLCertificate querySSLByServiceId(String serviceId) throws Exception;

	/**
	 * 删除ssl证书
	 * @param serviceId
	 */
	public void deleteSSLCertificate(String serviceId);

	/**
	 * 根据节点id查询lua代码
	 * @param serviceId
	 * @return
	 * @throws Exception 
	 */
	public ServiceLua queryLuaScriptByServiceId(String serviceId) throws Exception;

	/**
	 * 根据节点IP和PORT获取节点
	 * @param id
	 * @param ip
	 * @param parseInt
	 * @return
	 */
	public Service getAppServiceByIpAndPort(String id, String ip, int parseInt);

	/**
	 * 查询已关注的服务节点数量和全部的服务节点数量
	 * @return
	 */
	public Map<String, Object> queryServiceFocusCount() throws Exception;

	/**
	 * 服务节点命名
	 * @param service
	 * @throws ApmDataIndicatorException 
	 * @throws ServiceNotExistException 
	 */
	public void setServiceName(Service service) throws Exception;

	/**
	 * 获取自动发现的服务节点
	 * @param service
	 * @param serviceType
	 * @return
	 */
	public List<Service> getDisServiceFocusOrNot(Service service, String serviceType, String sortByMysql, boolean asc, int pageSize, int pageIndex, int severity, Date startDate, Date endDate);

	/**
	 * 过滤节点IP
	 * @param service
	 */
	public Service updateServiceIps(String appId, Service service) throws Exception;
	
	/**
	 * 保存luaSettings
	 * @return
	 */
	public Result saveLuaSettings(LuaSettings settings);
	
	/**
	 * 查看luaSettings
	 * @return
	 */
	public Result queryLuaSettings(@QueryParam("serviceId") String serviceId);
}
